/*
 *  Planet.cpp
 *  SpaceTrader
 *
 *  Created by Andrew on 10/16/12.
 *  Copyright 2012 The Hex Pistols. All rights reserved.
 *
 */

#include "Planet.h"
//for deserialization
Planet::Planet() : name(""), x(0), y(0), icon(NULL), market(NULL), techLevel(static_cast<tech_level_t>(0)),
                        resources(static_cast<resources_t>(0)), worldEvent(static_cast<world_event_t>(0)) { }

Planet::Planet(std::string newName, int newX, int newY, SDL_Surface *newIcon, Market *market)
                    : name(newName), x(newX), y(newY), icon(newIcon), market(market), techLevel(static_cast<tech_level_t> (rand()%TECHCOUNT)),
                    resources(static_cast<resources_t> (rand()%RESOURCESCOUNT)), worldEvent(static_cast<world_event_t> (rand()%EVENTCOUNT))
{
	std::vector<std::string> itemList = market->getItemList();
	for (int i = 0; i < GOODCOUNT; i++) {
		TradeGood *good = market->getTradeGood(itemList[i]);
		inventory[i] = (good->getMinProduce() <= techLevel) ? rand()%25 : 0;
	}
}

Planet::Planet(std::string newName, int newX, int newY, tech_level_t newTechLevel, resources_t newResources, world_event_t newWorldEvent,
                SDL_Surface *newIcon, Market *market)
                    : name(newName), x(newX), y(newY), icon(newIcon), market(market), techLevel(newTechLevel), resources(newResources), worldEvent(newWorldEvent)
                      { }

Planet::~Planet() {
	free(icon);
}

Planet::Planet(const Planet &P) : name(P.name), x(P.x), y(P.y), icon(P.icon), market(P.market), techLevel(P.techLevel),
                                    resources(P.resources), worldEvent(P.worldEvent)  { }

Planet& Planet::operator=(const Planet &P){
    if (&P == this) return *this;
    else {
        delete icon;
        delete market;
        name = P.name;
        x = P.x;
        y = P.y;
        icon = P.icon;
        market = P.market;
        techLevel = P.techLevel;
        resources = P.resources;
        worldEvent = P.worldEvent;
        return *this;
    }
}

std::string Planet::getName(){
	return name;
}

tech_level_t Planet::getTechLevel() {
	return techLevel;
}

resources_t Planet::getResources() {
	return resources;
}

world_event_t Planet::getWorldEvent() {
	return worldEvent;
}

int Planet::getX() {
	return x;
}

int Planet::getY() {
	return y;
}

void Planet::show(SDL_Surface *screen) {
	apply_surface(x, y, icon, screen);
}

char *Planet::techLevelToString()
{
    switch (techLevel){
        case PREAGRICULTURE: return "Pre-agricultural";
        case EARLYINDUSTRY: return "Early Industrial";
        case AGRICULTURE: return "Agricultural";
        case MEDIEVAL: return "Medieval";
        case RENAISSANCE: return "Renaissance";
        case INDUSTRY: return "Industrial";
        case POSTINDUSTRY: return "Post-industrial";
        case HITECH: return "High Tech";
		default: return NULL;
    }
}

char *Planet::resourcesLevelToString()
{
    switch(resources){
        case ARTISTIC: return "Artistic";
        case DESERT: return "Desert";
        case LIFELESS: return "Lifeless";
        case LOTSOFHERBS: return "Lots of herbs";
        case LOTSOFWATER: return "Lots of water";
        case MINERALPOOR: return "Mineral poor";
        case MINERALRICH: return "Mineral rich";
        case NOSPECIALRESOURCES: return "No special resources";
        case POORSOIL: return "Poor soil";
        case RICHFAUNA: return "Rich fauna";
        case RICHSOIL: return "Rich soil";
        case WARLIKE: return "Warlike";
        case EXOTICPLANTS: return "Exotic Plants";
		default: return NULL;
    }
}

char *Planet::worldEventToString()
{
	switch (worldEvent) {
		case NOEVENT: return "None";
		case DROUGHT: return "Drought";
		case COLD: return "Blizzard";
		case CROPFAIL: return "Crop Failure";
		case WAR: return "Civil war";
		case BOREDOM: return "Boredom";
		case PLAGUE: return "Plague";
		case LACKOFWORKERS: return "Union Strike";
		default: return NULL;
	}
}

int* Planet::getInventory() 
{
	return inventory;
}

